<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    gameTypeList:[] as Array<any>,
  })
  interface formQuert {
    id:number
    custom_id:number
    jump_url:string
    img_url:string
    game_type:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    jump_url: [{ required: true, message: "请输入跳转链接", trigger: "blur" }],
    game_type: [{ required: true, message: "请选择游戏类型", trigger: "blur" }],
    img_url: [{ required: true, message: "请上传图片", trigger: "blur" }],

  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id } = form
      if(id) {
        await gamesApi.customTicketGameEdit(form).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      else {
        await gamesApi.customTicketGameAdd(form).then(()=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = async(query:any={})=> {
    const {
      id=0, custom_id=0, jump_url='', img_url='', game_type='', gameTypeList=[]
    } = query
    if(!custom_id) return ToastWarning('缺少活动ID')
    const obj = {
      custom_id, jump_url, img_url, game_type,
    } as any
    if(id) obj['id'] = id
    Object.assign(form,obj)
    action.gameTypeList = gameTypeList
    action.dialogFormVisible = true
    action.title = id?`编辑活动游戏`:'创建活动游戏'
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="55%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="游戏类型" prop="game_type">
          <el-select v-model="form.game_type" placeholder="请选择游戏类型">
            <el-option
              v-for="item in action.gameTypeList"
              :label="item.game_name"
              :value="item.game_type"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="图片" prop="img_url">
          <xmUpload v-model:file-url="form.img_url" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="跳转链接" prop="jump_url">
          <el-input
            :disabled="action.disabled"
            v-model="form.jump_url"
            :autosize="{ minRows: 4, maxRows: 4}"
            type="textarea"
            placeholder="请输入跳转链接"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>